package com.ajouroid.smartcoworker;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.ajouroid.smartcoworker.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.support.v4.app.NavUtils;

public class ExplorerActivity extends ListActivity {

	List<String> mItem = null;
	List<String> mPathList = null;
	
	String root = "/sdcard/";
	
	String curPath = "/sdcard/";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_explorer);
        
        moveTo(root);
    }
    
    public void moveTo(String path)
    {
    	mItem = new ArrayList<String>();
    	mPathList = new ArrayList<String>();
    	
    	ArrayList<File> mDirectories = new ArrayList<File>();
    	ArrayList<File> mFiles = new ArrayList<File>();
    	
    	File f = new File(path);
    	File[] files = f.listFiles();
    	
    	if (!path.equals(root))
    	{    		
    		mItem.add("../");
    		mPathList.add(f.getParent());
    	}
    	
    	 for(int i=0; i < files.length; i++){
    	      File file = files[i];
    	      //mPathList.add(file.getPath());
    	      
	    	 if(file.isDirectory())
	    	 {
	    		 mDirectories.add(file);
	    		 //mItem.add(file.getName() + "/");
	    	 }
	    	 else
	    	 {
	    		 mFiles.add(file);
	    		 //mItem.add(file.getName());
	    	 }
    	 }
    	 
    	 Collections.sort(mDirectories, new Comparator<File>() {

			public int compare(File lhs, File rhs) {
				return (lhs.getName().compareTo(rhs.getName()));
			}
    		 
    	 });
    	 
    	 Collections.sort(mFiles, new Comparator<File>() {

 			public int compare(File lhs, File rhs) {
 				return (lhs.getName().compareTo(rhs.getName()));
 			}
     		 
     	 });
    	 
    	 for (int i=0; i<mDirectories.size(); i++)
    	 {
    		 mItem.add(mDirectories.get(i).getName() + "/");
    		 mPathList.add(mDirectories.get(i).getPath());
    	 }
    	 
    	 for (int i=0; i<mFiles.size(); i++)
    	 {
    		 String fName = mFiles.get(i).getName();
    		 if (fName.endsWith(".ppt") || fName.endsWith(".pptx") || fName.endsWith(".pdf") || fName.endsWith(".doc"))
    		 {
	    		 mItem.add(fName);
	    		 mPathList.add(mFiles.get(i).getPath());
    		 }
    	 }
    	 
    	 ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mItem);
         setListAdapter(fileList); 
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_explorer, menu);
        return true;
    }

    
    
    @Override
	public void onBackPressed() {
		if (!curPath.equals(root))
		{
			File directory = new File(curPath);
			moveTo(directory.getParent());
		}
		
		super.onBackPressed();
	}


	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		curPath = mPathList.get(position);
		
		File f = new File(curPath);
		
		if (f.isDirectory())
		{
			if (f.canRead())
			{
				moveTo(curPath);
			}
		}
		else
		{
			Intent info = new Intent();
			info.putExtra("path", curPath);
			setResult(RESULT_OK, info);
			finish();
		}

		super.onListItemClick(l, v, position, id);
	}

    
}
